﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Felbot.Interaction;

namespace Felbot {
    /// <summary>A little form that allows the user to store and use a list of CD-keys.</summary>
    public partial class frmGroupsSelector : Form {
        frmUserEntryManager m_Manager;
        XmlEntry m_Entry;

        /// <summary>Creates a new flags form.</summary>
        /// <param name="manager">The User Entry Manager that opened it.</param>
        /// <param name="entry">The entry.</param>
        public frmGroupsSelector(frmUserEntryManager manager, XmlEntry entry) {
            InitializeComponent();
            m_Manager = manager;
            m_Entry = entry;
            LoadGroupsList();
        }

        private void LoadGroupsList() {
            List<string> checkedGroups = UserEntryControl.GetGroupsContaining(m_Entry);
            foreach (XmlEntry entry in UserEntryControl.EntryList) {
                if (entry.EntryType == XmlEntryType.Group) {
                    ListViewItem item = lvGroups.Items.Add(new ListViewItem(entry.Name));
                    if (checkedGroups.Contains(entry.Name)) {
                        item.Checked = true;
                    }
                }
            }
        }

        private void frmGroupsSelector_FormClosing(object sender, FormClosingEventArgs e) {
            List<string> groups = new List<string>();
            foreach (ListViewItem item in lvGroups.Items)
                if (item.Checked)
                    groups.Add(item.Text);
            m_Manager.SetGroups(groups);
        }

        private void btnDone_Click(object sender, EventArgs e) {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
